<?php
App::uses('AppController', 'Controller');
/**
 * Managedfunds Controller
 *
 * @property Managedfund $Managedfund
 */
class ManagedfundsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		/* cakephp original codes
		$this->Managedfund->recursive = 0;
		$this->set('managedfunds', $this->paginate());
		*/
		include("Component/connection.php");
		$sql="SELECT DISTINCT fundname From managedfunds ORDER BY fundname";
		$temp=mysqli_query($con,$sql);
		$counter=0;
		foreach ($temp as $temp2)
		{
			foreach($temp2 as $temp3)
			{
				//get fund name
				$this->Session->write("fundname[".$counter."]",$temp3);
				//get apir code
				$sql2="SELECT apircode FROM managedfunds where fundname='".$temp3."' order by date desc limit 1";
				$tempAPIR=mysqli_query($con,$sql2);
				foreach ($tempAPIR as $tapir)
				{
					foreach ($tapir as $tr)
					{
						$this->Session->write("fundapir[".$counter."]",$tr);
					}
				} 
				$tempAPIR->close();
				//get fund id
				$sql2="SELECT fundsid FROM managedfunds where fundname='".$temp3."' order by date desc limit 1";
				$tempID=mysqli_query($con,$sql2);
				foreach ($tempID as $tID)
				{
					foreach ($tID as $td)
					{
						$this->Session->write("fundid[".$counter."]",$td);
					}
				} 
				$tempID->close();
				$counter ++;
			}

		}
		$this->Session->write("counter",$counter);
		//* $temp->close();
		//* $con->close();
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Managedfund->exists($id)) {
			throw new NotFoundException(__('Invalid managedfund'));
		}
		$options = array('conditions' => array('Managedfund.' . $this->Managedfund->primaryKey => $id));
		$this->set('managedfund', $this->Managedfund->find('first', $options));

		$con=mysqli_connect("localhost","root","","dfs");
		$sql="SELECT fundname from managedfunds where fundsid = '".$id."'";
		$temp=mysqli_query($con,$sql);
		foreach ($temp as $temp2)
		{
			foreach($temp2 as $temp3)
			{
				$fundname=$temp3;
			}

		}
		$temp->close();

		$sql2="SELECT fundsid,date,OneMonthReturn,ThreeMonthReturn,SixMonthReturn,OneYearReturn,TwoYearReturn,ThreeYearReturn,FourYearReturn,FiveYearReturn,SinceInceptionReturn from managedfunds where fundname='".$fundname."' order by date desc limit 10";
		$tempRecord=mysqli_query($con,$sql2);

		$counter=0;
		foreach ($tempRecord as $tempR)
		{
			//debug($tempR);
			$counter2=0;
			foreach($tempR as $tr)
			{
				$this->Session->write("record[".$counter."][".$counter2."]",$tr);
				$counter2++;
			}
			$counter++;
			$this->Session->write("recordPer",$counter2);
		}

		$this->Session->write("recordTotal",$counter);
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Managedfund->create();
			if ($this->Managedfund->save($this->request->data)) {
				$this->Session->setFlash(__('The Managed Fund record has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Managed Fund record could not be saved. Please, try again.'));
			}
		}

		$this->loadModel('Apircode');
		$apir = $this->Apircode->find('list');
		$this->set(compact('apir'));

		$this->loadModel('Portfolioname');
		$portfolios = $this->Portfolioname->find('list');
		$this->set(compact('portfolios'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Managedfund->exists($id)) {
			throw new NotFoundException(__('Invalid managedfund'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Managedfund->save($this->request->data)) {
				$this->Session->setFlash(__('The Managed Fund record has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Managed Fund record could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Managedfund.' . $this->Managedfund->primaryKey => $id));
			$this->request->data = $this->Managedfund->find('first', $options);
		}

		$this->loadModel('Apircode');
		$apir = $this->Apircode->find('list');
		$this->set(compact('apir'));

		$this->loadModel('Portfolioname');
		$portfolios = $this->Portfolioname->find('list');
		$this->set(compact('portfolios'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Managedfund->id = $id;
		if (!$this->Managedfund->exists()) {
			throw new NotFoundException(__('Invalid managedfund'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Managedfund->delete()) {
			$this->Session->setFlash(__('The Managed Fund record is deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Managed Fund record was not deleted'));
		$this->redirect(array('action' => 'index'));
	}




}


